/**
 * this plugin get all values from one form and forms a query string
 * the elements in the form need a id
 * to use this plugin you have to import common.js, because you need the function
 * getEncodedParams()
 * 
 * exaple:
 * 		<form id="test">
 * 			<input id="textbox" type="text" value="testValue" />
 * 			...
 * 		</form>
 * 
 * $('#test').getFormParams(); -> 'TEXTBOX=testValue'
 *
 * @author		simon ungerboeck <ungerboeck_simon@hotmail.com>
 * @version		1.0
 */

(function($){
	$.fn.extend({
		getFormParams : function(){
			var queryParams = {};
			var elements = $('input,textarea,select:not(.chzn-select)', this); 		//only params from those elements

			elements.each(function(){			//loop for all selected elements
				if ($(this).attr('id') !== undefined){
					switch ($(this).context.tagName.toUpperCase()){
						case 'INPUT':
							getInputValues($(this), queryParams);
							break;
						case 'SELECT':
							getSelectValues($(this), queryParams);
							break;
						case 'TEXTAREA':
							getTextAreaValues($(this), queryParams);
							break;
					}
				}
			});
			return(getEncodedParams(queryParams));
		},
		getRawFormParams: function() {
			var queryParams = {};
			var elements = $('input,textarea,select:not(.chzn-select)', this); 		//only params from those elements

			elements.each(function(){			//loop for all selected elements
				switch ($(this).context.tagName.toUpperCase()){
				case 'INPUT':
					getInputValues($(this), queryParams);
					break;
				case 'SELECT':
					getSelectValues($(this), queryParams);
					break;
				case 'TEXTAREA':
					getTextAreaValues($(this), queryParams);
					break;
				}
			});
			return queryParams;
		}
	});


	function getParams($this){

	}


	/**
	 * returns all values from tag from type select
	 * @param		mixed		obj				html dom object
	 * @return		string		queryString
	 */
	function getSelectValues(obj, queryParams){
		var size = obj.attr('size');

		if (size == null || size == undefined || size == 1)			//if element is a combobox
			queryParams[obj.attr('id').toUpperCase()] = $('option:selected', obj).val();
		else {														//if element is a listbox
			var values = '';
			$('option', obj).each(function(){
				values +=  $(this).val() + ',';
			})
			values = values.substring(0, values.length-1);		//remove the last ','
			queryParams[obj.attr('id').toUpperCase()] = values;
		}
		return queryParams;
	}


	/**
	 * returns all values from tag from type inut
	 * @param		mixed		obj				html dom object
	 * @return		string		queryString
	 */
	function getInputValues(obj, queryParams){
		if (obj.attr('type') != null){
			switch((obj.attr('type')).toUpperCase()){
			case 'TEXT':
			case 'PASSWORD':
			case 'HIDDEN':
				if (obj.attr('id') != null)
					queryParams[obj.attr('id').toUpperCase()] = obj.val();
				break;
			case 'RADIO':
				if (obj.attr('checked') == 'checked')
					queryParams[obj.attr('name').toUpperCase()] = obj.val();
				break;
			case 'CHECKBOX':
				if (obj.attr('id') !== undefined){
					if (obj.attr('checked') == 'checked')
						queryParams[obj.attr('id').toUpperCase()] = 1;
					else
						queryParams[obj.attr('id').toUpperCase()] = 0;
				}
				break;
			}

		}
		return queryParams;
	};


	function getTextAreaValues(obj, queryParams){
		if (obj.attr('id') != null)
			queryParams[obj.attr('id').toUpperCase()] = obj.val();
		return queryParams;
	}

})(jQuery);